'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 获取属性列表
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var genRewardService = require('./genRewardService');
var vipConf = require('../config/vipConf');

/**
 * 获取属性列表
 * @param  {[type]} equip_type [description]
 * @return {[type]}            [description]
 */
exports.getPropertyList = function (bag_model) {
    var equip_list = bag_model.getWearEquipList();
    return _getEquipListProperty(equip_list);
};

exports.getPropertyListByEquipList = function (equip_list) {
    return _getEquipListProperty(equip_list);
};

/**
 * 获取装备的属性
 * @param  {[type]} equip_list [description]
 * @return {[type]}            [description]
 */
var _getEquipListProperty = function (equip_list) {
    var property_list = {};

    // 初始化属性列表
    dataApi.attribute_type.each(function (value, key) {
        property_list[key] = {};
        property_list[key].property_type = key;
        property_list[key].value1 = 0;
        property_list[key].value2 = 0;
    });

    // 遍历所有装备数据
    _.each(equip_list, function (equip_item) {
        // 叠加装备属性
        property_list = _getEquipProperty(property_list, equip_item.getType(), equip_item.getLv(), equip_item.getRefineLv());
    });
    return property_list;
};

/**
 * 计算装备属性
 * @param  {[type]} hero_attr  [description]
 * @param  {[type]} equip_type [description]
 * @param  {[type]} equip_lv   [description]
 * @return {[type]}            [description]
 */
var _getEquipProperty = function (property_list, equip_type, strength_lv, refine_lv) {
    var equip_type_conf = dataApi.equipment_type.findById(equip_type);
    var strength_lv_conf = dataApi.equipment_strengthen.findById(strength_lv);
    var equip_lv_conf = dataApi.equipment_lvl.findById(refine_lv);

    if (!equip_type_conf) {
        console.error('invalid equip_type:', equip_type);
    }

    var strength_val = strength_lv_conf ? strength_lv_conf.value : 0;
    var refine_val = equip_lv_conf ? equip_lv_conf.add_per : 0;

    equip_type_conf.value *= 1 + (strength_val + refine_val) / 100;
    equip_type_conf.value_2 *= 1 + (strength_val + refine_val) / 100;

    return _combineProperty(
        property_list,
        equip_type_conf.property,
        equip_type_conf.value,
        equip_type_conf.value_2);
};

/**
 * 合并属性
 * @param  {[type]} property_list [description]
 * @param  {[type]} property_type [description]
 * @param  {[type]} value1        [description]
 * @param  {[type]} value2        [description]
 * @return {[type]}               [description]
 */
var _combineProperty = function (property_list, property_type, value1, value2) {
    property_list[property_type].value1 += value1 || 0;
    property_list[property_type].value2 += value2 || 0;
    return property_list;
};
